<%-- 
    Document   : agenda
    Created on : 16/11/2013, 21:46:59
    Author     : TiagoVieira86
--%>

<%@page import="java.sql.PreparedStatement"%>
<%@page import="package01.Conexão"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" href="estilo.css" type="text/css"/>
        <title>Agenda - ZooControl</title>
    </head>
    <body>
        <%if(request.getParameter("nome")!=null){
        String nome = request.getParameter("nome");
        session.setAttribute("nome", nome);
        }else if(request.getParameter("sair")!= null){
        session.removeAttribute("nome");
        }
        %>
        <div class="header">
        <h1>Agendamento de Serviços e Eventos</h1>
        </div>
        <hr/>
        <%if(session.getAttribute("nome")!=null){%>
        <div class="menu">
            <a href="home.jsp">Voltar para a Página Principal</a>
            <hr/>
            <form action="home.jsp" method="post">
            <input type="hidden" name="sair"/>
            <input type="submit" value="Sair"/>
            </form>        
        </div>
        <div class="content">
            <form action="agenda" method="get">
                Cod.Item: <input type="text" name="CD_ITEM"/><br/>
                Fornecedor: <input type="text" name="NM_FORNECEDOR"/><br/>
                Quantidade: <input type="number" name="QT_ITEM"/><br/><br/>
                <label for="r1">Utilização: </label></br>
                <label><input type="radio" name="ec1" id="r1" value="Animal"/>Animal</label></br>
                <label><input type="radio" name="ec1" id="r1" value="Especie"/>Espécie</label></br>
                <label><input type="radio" name="ec1" id="r1" value="Habitat"/>Habitat</label></br>
                <input type="submit" value="Adicionar Agendamento">    
            </form>
            <hr/>
                    <%
            Connection Con = null;
            Statement stmt = null;
            ResultSet rs = null;
            try{
            Con = Conexão.getConnection("zoocontrol", "zoocontrol");
            if(request.getParameter("CD_ITEM") != null){
            String insertSQL = "INSERT INTO AGENDA VALUES(?,?,?)";
            PreparedStatement ps = Con.prepareStatement(insertSQL);
            ps.setInt(1, Integer.parseInt(request.getParameter("REGION_ID")));
            ps.setString(2, request.getParameter("REGION_NAME"));
            ps.execute();
            ps.close();
            }else if(request.getParameter("excluirID") != null){
            String insertSQL = "DELETE FROM REGIONS WHERE REGION_ID = ?";
            PreparedStatement ps = Con.prepareStatement(insertSQL);
            ps.setInt(1, Integer.parseInt(request.getParameter("excluirID")));
            ps.execute();
            ps.close();
            }
        %>
        <table border="1">
            <tr>
                <th>-</th>
                <th>ID</th>
                <th>Nome</th>
            </tr>
            <%
            stmt = Con.createStatement();
            rs = stmt.executeQuery("SELECT * FROM REGIONS");
            while(rs.next()){
            String id = rs.getString("REGION_ID");
            out.println("<tr>");
            out.println("<td><a href='?excluirID=" + id + "'>Excluir</a></td>");
            out.println("<td>" + id + "</td>");
            out.println("<td>"+rs.getString("REGION_NAME")+"</td>");
            out.println("</tr>");
            }
            }catch(Exception ex){
            out.println("<tr><td colspan='3'>ERRO: "+ ex.getMessage() +"</td></tr>");
            }finally{
            try{rs.close();}catch(Exception e){}
            try{stmt.close();}catch(Exception e){}
            try{Con.close();}catch(Exception e){}
            }
            %>
        </table>

        </div>
        <%}else{%>
        <h3>Usuário não autorizado</h3>
        <h3><a href="home.jsp">Faça login para acessar.</a></h3>
        <%}%>
    </body>
</html>
